package com.markerhub.controller;

import com.markerhub.common.lang.Result;
import com.markerhub.entity.SysAnnouncement;
import com.markerhub.service.SysAnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 系统公告Controller
 */
@Api(tags = "系统公告管理")
@RestController
@RequestMapping("/api/sys-announcement")
public class SysAnnouncementController extends BaseController {

    @Autowired
    private SysAnnouncementService sysAnnouncementService;

    /**
     * 获取系统公告列表
     */
    @PreAuthorize("hasAuthority('sys:announcement:list')")
    @ApiOperation(value = "获取公告列表", notes = "获取所有系统公告信息，支持分页查询")
    @GetMapping("/list")
    public Result list(@RequestParam(required = false) String title,
                      @RequestParam(required = false) Integer type) {
        // 使用BaseController中的getPage()方法获取分页参数
        Page<SysAnnouncement> page = getPage();
        // 构建查询条件
        QueryWrapper<SysAnnouncement> wrapper = new QueryWrapper<>();
        if (title != null && !title.isEmpty()) {
            wrapper.like("title", title);
        }
        if (type != null) {
            wrapper.eq("type", type);
        }
        // 按创建时间倒序排序
        wrapper.orderByDesc("created");
        // 执行分页查询
        Page<SysAnnouncement> pageData = sysAnnouncementService.page(page, wrapper);
        return Result.succ(200, "获取系统公告列表成功", pageData);
    }

    /**
     * 查询系统公告
     */
    @PreAuthorize("hasAuthority('sys:announcement:list')")
    @ApiOperation(value = "查询系统公告", notes = "根据条件查询系统公告")
    @GetMapping("/query")
    public Result query() {
        List<SysAnnouncement> list = sysAnnouncementService.list();
        return Result.succ(200, "查询系统公告成功", list);
    }

    /**
     * 根据ID获取系统公告信息
     */
    @PreAuthorize("hasAuthority('sys:announcement:list')")
    @ApiOperation(value = "获取公告详情", notes = "根据ID获取系统公告详细信息")
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable Long id) {
        SysAnnouncement announcement = sysAnnouncementService.getById(id);
        return Result.succ(200, "获取系统公告信息成功", announcement);
    }

    /**
     * 创建系统公告
     */
    @PreAuthorize("hasAuthority('sys:announcement:add')")
    @ApiOperation(value = "创建系统公告", notes = "新增系统公告信息")
    @PostMapping("/add")
    public Result add(@RequestBody SysAnnouncement announcement) {
        // 设置创建时间
        announcement.setCreated(new Date());
        // 设置更新时间
        announcement.setUpdated(new Date());
        boolean saved = sysAnnouncementService.save(announcement);
        return saved ? Result.succ(200, "创建系统公告成功", null) : Result.fail("创建系统公告失败");
    }

    /**
     * 更新系统公告
     */
    @PreAuthorize("hasAuthority('sys:announcement:update')")
    @ApiOperation(value = "更新系统公告", notes = "更新系统公告信息")
    @PutMapping("/update")
    public Result update(@RequestBody SysAnnouncement announcement) {
        // 设置更新时间
        announcement.setUpdated(new Date());
        boolean updated = sysAnnouncementService.updateById(announcement);
        return updated ? Result.succ(200, "更新系统公告成功", null) : Result.fail("更新系统公告失败");
    }

    /**
     * 删除系统公告（单个）
     */
    @PreAuthorize("hasAuthority('sys:announcement:delete')")
    @ApiOperation(value = "删除系统公告", notes = "根据ID删除单个系统公告")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        boolean deleted = sysAnnouncementService.removeById(id);
        return deleted ? Result.succ(200, "删除系统公告成功", null) : Result.fail("删除系统公告失败");
    }
    

}